# Converts clipboard file list to plain text.
# Input: file list.
# Output: list of file names as plain text.
# Should be run with JRuby.
# Copyright (c) Nikolai Holub.

require 'java'
include_class 'java.awt.Toolkit'
include_class 'java.awt.datatransfer.DataFlavor'
include_class 'java.awt.datatransfer.StringSelection'

clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();

contents = clipboard.getContents(nil)

if (contents.isDataFlavorSupported(DataFlavor.javaFileListFlavor))
  list = contents.getTransferData(DataFlavor.javaFileListFlavor)

  text = String.new
  list.each { |file|
    text.concat(file.name + "\n")
  }

  puts text
  clipboard.setContents(StringSelection.new(text), nil)
end
